(function (vc) {
    vc.extends({
        data: {
            dispatchHousekeepingInfo: {
                poolId: '',
                servId: '',
                staffId: '',
                staffName: '',
                context: '',
                action: '',
                servUsers: []
            }
        },
        _initMethod: function () {
        },
        _initEvent: function () {
            vc.on('dispatchHousekeeping', 'openDispatchHousekeepingModal', function (_pool) {
                $that.dispatchHousekeepingInfo.servId = _pool.servId;
                $that.dispatchHousekeepingInfo.poolId = _pool.poolId;
                $that.dispatchHousekeepingInfo.action = _pool.action;
                $that._listServUsers();
                if (_pool.hasOwnProperty('action') && _pool.action == 'BACK') {
                    $that.dispatchHousekeepingInfo.staffId = _pool.preStaffId;
                    $that.dispatchHousekeepingInfo.staffName = _pool.preStaffName;
                }
                $('#dispatchHousekeepingModel').modal('show');
            });
        },
        methods: {
            dispatchHousekeepingValidate() {
                return vc.validate.validate({
                    dispatchHousekeepingInfo: vc.component.dispatchHousekeepingInfo
                }, {
                    'dispatchHousekeepingInfo.poolId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "报修单不能为空"
                        }
                    ],
                    'dispatchHousekeepingInfo.staffId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "报修师傅不能为空"
                        }
                    ],
                    'dispatchHousekeepingInfo.staffName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "报修师傅不能为空"
                        }
                    ],
                    'dispatchHousekeepingInfo.context': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "处理意见不能为空"
                        }
                    ]
                });
            },
            _dispatchHousekeepingInfo: function () {
                $that.dispatchHousekeepingInfo.servUsers.forEach(item => {
                    if (item.staffId == $that.dispatchHousekeepingInfo.staffId) {
                        $that.dispatchHousekeepingInfo.staffName = item.staffName;
                    }
                });
                if (!vc.component.dispatchHousekeepingValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                vc.component.dispatchHousekeepingInfo.shopId = vc.getCurrentCommunity().shopId;
                vc.http.apiPost(
                    '/housekeepingServPoolUser/poolDispatch',
                    JSON.stringify(vc.component.dispatchHousekeepingInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#dispatchHousekeepingModel').modal('hide');
                            vc.component.clearDispatchHousekeepingInfo();
                            vc.emit('housekeepingServPoolManage', 'listHousekeepingServPool', {});
                            vc.emit('housekeepingDispatchManage', 'listHousekeepingPool', {});
                            vc.toast("操作成功");
                        } else if (_json.code == 404) {
                            vc.toast(_json.msg);
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            clearDispatchHousekeepingInfo: function () {
                vc.component.dispatchHousekeepingInfo = {
                    poolId: '',
                    servId: '',
                    staffId: '',
                    staffName: '',
                    context: '',
                    action: '',
                    servUsers: []
                };
            },
            _listServUsers: function () {
                var param = {
                    params: {
                        page: 1,
                        row: 50,
                        shopId: vc.getCurrentCommunity().shopId,
                        servId: $that.dispatchHousekeepingInfo.servId,
                        state: '9999'
                    }
                };
                //发送get请求
                vc.http.apiGet('/housekeepingServUser/queryHousekeepingServUser',
                    param,
                    function (json, res) {
                        var _poolTypeUserManageInfo = JSON.parse(json);
                        vc.component.dispatchHousekeepingInfo.servUsers = _poolTypeUserManageInfo.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
        }
    });
})(window.vc);
